package huoye.shops.huoye.operation.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.huoye.operation.model.EventOddPrizeList;
import huoye.shops.huoye.operation.model.ManageEventData;
import huoye.shops.huoye.operation.model.ShopEventPrizeList;
import huoye.shops.huoye.user.model.User;
import huoye.shops.widget.RoundImageView;


/**
 * Created by uesr on 2018/8/10.
 */

public class MineEventSetAdapter extends RecyclerViewAdapter<MineEventSetAdapter.ViewHolder,ShopEventPrizeList> {


    public MineEventSetAdapter(Context context) {
        super(context, R.layout.item_mine_event_set);

    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    private int maxCount;

    @Override
    protected ViewHolder initViewHolder(View view, int i) {
        return new ViewHolder(view);
    }

    @Override
    protected void bindView(final ViewHolder viewHolder, final ShopEventPrizeList list, final int positon) {

        EditText[] inputEts = {viewHolder.etPordOne, viewHolder.etPordTwo, viewHolder.etName, viewHolder.etNumber};
        for (EditText et : inputEts) {
            if (et.getTag() != null && et.getTag() instanceof TextWatcher) {
                et.removeTextChangedListener((TextWatcher) et.getTag());
            }
        }

        showImg(viewHolder, list, positon);
        if (positon == getItemCount() - i) return;
        for (int i = 0; i < inputEts.length; i++) {
            final EditText et = inputEts[i];
            final int index = i;
            TextWatcher tvWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (index == 0) {
                        list.setBeginNum(TextUtils.isEmpty(s) ? "" : s.toString());
                    } else if (index == 1) {
                        list.setEndNum(TextUtils.isEmpty(s) ? "" : s.toString());
                    } else if (index == 2) {
                        list.setPrizeTitle(TextUtils.isEmpty(s) ? "" : s.toString());
                    } else if (index == 3) {
                        list.setNum(TextUtils.isEmpty(s) ? "" : s.toString());
                    }
                }
            };
            et.addTextChangedListener(tvWatcher);
            et.setTag(tvWatcher);
        }


        list.setPrizeImg(TextUtils.isEmpty(list.getPrizeImg()) ? "" : list.getPrizeImg());

   /*     viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData().remove(positon);
                notifyDataSetChanged();
            }

        });*/


        viewHolder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRecyclerViewItemClickListener.onAddImageClick(positon);
            }
        });

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            return new ViewHolder((View) this.getHeaderViews().get(0));
        } else if (viewType == -2) {
            return new ViewHolder((View) this.getHeaderViews().get(0));
        } else {
            ViewHolder v = this.onCreateViewDataHolder(this.mLayoutInflater, parent, viewType);
            if (v != null && v.itemView != null) {
                v.itemView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        int position = (Integer) view.getTag();
                        if (onRecyclerViewItemClickListener != null && position < getItemCount() - 1) {
                            onRecyclerViewItemClickListener.onItemClick(getData().get(position), position, view);
                        } else if (onRecyclerViewItemClickListener != null && position == getItemCount() - 1) {
                            onRecyclerViewItemClickListener.onItemClick(null, position, view);
                        }

                    }
                });
            }

            return v;
        }
    }


    public ViewHolder onCreateViewDataHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        View view = this.createHolderView(inflater, parent, viewType);
        return this.initViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (this.getItemViewType(position) != -1) {
            if (this.getItemViewType(position) == -2) {
                holder.itemView.setVisibility(View.VISIBLE);
            } else {
                int pos = this.getRealPosition(holder);
                if (pos != this.getListCount()) {
                    ShopEventPrizeList data = getData().get(pos);
                    if (data != null) {
                        holder.itemView.setTag(pos);
                        this.bindView(holder, data, pos);
                    }
                } else {
                    holder.itemView.setTag(pos);
                    this.bindView(holder, null, pos);
                }
            }
        }
    }

    int i = 1;

    private void showImg(final ViewHolder viewHolder, final ShopEventPrizeList list, final int positon) {
        if (getListCount() == maxCount) {
            //6张图
            if (positon == maxCount) {
              //  viewHolder.ivDelete.setVisibility(View.GONE);
                viewHolder.rvAdd.setVisibility(View.INVISIBLE);
            } else {
             //   viewHolder.ivDelete.setVisibility(View.VISIBLE);
                viewHolder.rvAdd.setVisibility(View.VISIBLE);
                viewHolder.ivAdd.setVisibility(View.INVISIBLE);
                if(StringUtils.isNotEmpty(list.getPrizeImg())){
                    Glide.with(getContext())
                            .load(new File(list.getPrizeImg()))
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                }else {
                    viewHolder.iv.setImageResource(R.mipmap.ic_add_image);
                }

                viewHolder.etName.setText(list.getPrizeTitle());
                viewHolder.etNumber.setText(list.getNum());
                viewHolder.etPordOne.setText(list.getBeginNum());
                viewHolder.etPordTwo.setText(list.getEndNum());

            }
        } else {
            // 有5张图片。但是 你这里吧最后一张图片换成了 add图标
            if (positon == getItemCount() - i) {
              //  viewHolder.ivDelete.setVisibility(View.GONE);
                viewHolder.rvAdd.setVisibility(View.INVISIBLE);
                viewHolder.ivAdd.setVisibility(View.VISIBLE);


            } else {
                viewHolder.rvAdd.setVisibility(View.VISIBLE);
                viewHolder.ivAdd.setVisibility(View.INVISIBLE);
             //   viewHolder.ivDelete.setVisibility(View.VISIBLE);
                if(StringUtils.isNotEmpty(list.getPrizeImg())){
                    Glide.with(getContext())
                            .load(new File(list.getPrizeImg()))
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .placeholder(R.mipmap.ic_wait_logo)
                            .error(R.mipmap.ic_error)
                            .into(viewHolder.iv);
                }else {
                    viewHolder.iv.setImageResource(R.mipmap.ic_add_image);
                }

                viewHolder.etName.setText(list.getPrizeTitle());
                viewHolder.etNumber.setText(list.getNum());
                viewHolder.etPordOne.setText(list.getBeginNum());
                viewHolder.etPordTwo.setText(list.getEndNum());

            }
        }
    }


    @Override
    public void add(ShopEventPrizeList list) {
        getData().add(list);
    }

    public static class ViewHolder extends RecyclerViewAdapter.ViewHolder {

        @BindView(R.id.item_mine_event_set_et_pord_one)
        EditText etPordOne;
        @BindView(R.id.item_mine_event_set_et_pord_two)
        EditText etPordTwo;
        @BindView(R.id.item_mine_event_set_et_name)
        EditText etName;
        @BindView(R.id.item_mine_event_set_et_number)
        EditText etNumber;
        @BindView(R.id.item_mine_event_set_iv_add_image)
        ImageView iv;

    /*    @BindView(R.id.item_mine_event_set_iv_del)
        ImageView ivDelete;*/

        @BindView(R.id.item_mine_event_set_iv_add)
        ImageView ivAdd;


        @BindView(R.id.item_mine_event_set_rv_add)
        RelativeLayout rvAdd;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (getListCount() >= maxCount) {
            return maxCount;
        } else {
            return getData().size() + 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < this.getHeaderViews().size()) {
            return -1;
        } else {
            return position >= this.getHeaderViews().size() + this.getListCount() + 1 ? -2 : this.getDataItemViewType(position - this.getHeaderViews().size());
        }

    }


    protected OnItemClickListener onRecyclerViewItemClickListener;

    public interface OnItemClickListener<T> {
        void onItemClick(T var1, int var2, View var3);
        void onAddImageClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }



}

